<?php

/**
 * This is the model class for table "flat".
 *
 * The followings are the available columns in table 'flat':
 * @property integer $id
 * @property string $number
 * @property string $note
 * @property integer $block_id
 *
 * The followings are the available model relations:
 * @property Block $block
 */
class Flat extends ActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Flat the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'flat';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {

        return array(
                array('number, block_id', 'required', 'message'=>'Kötelező kitölteni {attribute}'),
                array('block_id', 'numerical', 'integerOnly'=>true),
                array('number', 'length', 'max'=>10, 'message'=>'Legfeljebb 10 karakter lehet.'),
                array('number', 'match', 'pattern'=> '/^([A-Za-z0-9_\/]*)$/', 'allowEmpty'=>false, 'message'=>'Csak betű, szám, / (per) és _ (aláhúzás) lehet.'),
                array('number', 'unique', 'criteria'=>array(
                        'condition'=>'block_id = :blockId',
                        'params'=>array(
                                ':blockId'=>$this->block->id
                        )
                ), 'message'=>'A házszám egyedi kell legyen.'),
                array('note', 'safe'),
                array('number, note', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array(
                'block' => array(self::BELONGS_TO, 'Block', 'block_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
                'id' => 'ID',
                'number' => 'Házszám',
                'note' => 'Megjegyzés',
                'block_id' => 'Háztömb',
        );
    }

    /**
     *
     * @param Block $block
     * @return Flat
     */
    public function byBlock($block)
    {
        $blockId = ($block instanceof Block) ? $block->id : $block;

        $this->getDbCriteria()->mergeWith(array(
                'condition' => 'block_id = :blockId',
                'params'=>array(':blockId'=> $blockId)
        ));

        return $this;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('number',$this->number,true);
        $criteria->compare('note',$this->note,true);

        return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
        ));
    }
}